package structure;

import Util.StdOut;

public class BracketChecker {

    private String input;
    
    public BracketChecker(String input) {
        this.input = input;
    }
    
    public void check() {
        int stackSize = input.length();
        Stack stack = new Stack(stackSize);
        
        for (int i = 0; i < stackSize; i++) {
            char ch = input.charAt(i);
            switch (ch) {
                case '{':
                case '[':
                case '(':
                    stack.push(ch);
                    break;
                case ']':
                case '}':
                case ')':
                    if (!stack.isEmpty()) {
                        char chx = stack.pop();
                        if ((ch == ']' && chx != '[') || (ch == '}' && chx != '{')
                            || (ch == ')' && chx != '(')) {
                            System.out.println("match error: " + ch + " at " + i);
                        }
                    } else {
                        System.out.println("match error: " + ch + " at " + i);
                    }
                    break;
                default: 
                    break;
            }
        }
        if (!stack.isEmpty()) {
            StdOut.println("match erro: missing the right delimiter");
        }
    }
}
